/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;
import DTO.*;
import UTIL.*;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
/**
 *
 * @author ldtung_it
 */
public class FileAttachedDAO {
    /**
     * add a fileAttached
     * @param fattached
     * @return
     */
    public static int add(FileAttachedDTO fattached) throws SQLException{
        Connection connect = null;
         try {
            connect = SqlDataAccessHelper.getConnection();
            Statement stmt=(Statement) connect.createStatement();
            String sqlQuery = "insert into FileAttached values(" + fattached.get_idMail() + ",'"+ fattached.get_pathFileAttached() + "' )";
            stmt.executeUpdate(sqlQuery);
            stmt.close();
            connect.close();
            return 1;
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Fail to connect or mistake to add!");
        }
         connect.close();
        return 0;
    }
    /**
     * delete a record with idmail or pathFileAttached.
     * @param idMail
     * @param pathFileAttached
     * @return
     */
    public static int delete(int idMail,String pathFileAttached) throws SQLException{
        Connection connect = null;
        try {
            connect = SqlDataAccessHelper.getConnection();
            Statement stmt=(Statement) connect.createStatement();
            String sqlQuery = "";
            if(idMail > 0 && pathFileAttached != null)
                sqlQuery = "delete from FileAttached where IDMail = " + idMail + " and PathFileAttached = '"+ pathFileAttached + "'";
            else if(idMail <= 0 && pathFileAttached != null)
                sqlQuery = "delete from FileAttached where PathFileAttached = '"+ pathFileAttached + "'";
            else if(idMail > 0 && pathFileAttached == null)
                sqlQuery = "delete from FileAttached where IDMail = " + idMail ;
            else if(idMail <= 0 && pathFileAttached == null)
                return 0;
            stmt.executeUpdate(sqlQuery);
            stmt.close();
            connect.close();
            return 1;
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Fail to connect or mistake to delete!");
        }
        connect.close();
        return 0;
    }
    /**
     * update a record with new data
     * @param idMail
     * @param pathFileAttached
     * @return
     */
    public static int update(int idMail,String pathFileAttached) throws SQLException{
        Connection connect = null;
        try {
            connect = SqlDataAccessHelper.getConnection();
            Statement stmt=(Statement) connect.createStatement();
            String sqlQuery = "update FileAttached set PathFileAttached = '" + pathFileAttached + "' where IDMail = " + idMail;
            stmt.executeUpdate(sqlQuery);
            stmt.close();
           connect.close();
            return 1;
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Fail to connect or mistake to update!");
        }
         connect.close();
        return 0;
    }
    //public static int retrieve()
            
}
